package com.sy.mall.autoserver.controller;

import com.alibaba.fastjson.TypeReference;
import com.sy.common.utils.R;
import com.sy.common.vo.MemberResponseVo;
import com.sy.mall.autoserver.feign.MemberFeignService;
import com.sy.mall.autoserver.vo.UserLoginVo;
import com.sy.mall.autoserver.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static com.sy.common.constant.AuthServerConstant.LOGIN_USER;

@Controller
public class LoginController {

    @Autowired
    MemberFeignService memberFeignService;

//    @GetMapping(value = "/login.html")
//    public String loginPage(HttpSession session) {
//
//        return "login";
//
////        //从session先取出来用户的信息，判断用户是否已经登录过了
////        Object attribute = session.getAttribute(LOGIN_USER);
////        //如果用户没登录那就跳转到登录页面
////        if (attribute == null) {
////            return "login";
////        } else {
////            return "redirect:http://mall.com";
////        }
//
//    }
//
//    @GetMapping(value = "/reg.html")
//    public String regPage(){
//        return "reg";
//    }


    /**
     *
     * TODO: 重定向携带数据：利用session原理，将数据放在session中。
     * TODO:只要跳转到下一个页面取出这个数据以后，session里面的数据就会删掉
     * TODO：分布下session问题
     * RedirectAttributes：重定向也可以保留数据，不会丢失
     * 用户注册
     * @return
     */
    @PostMapping(value = "/regist")
    public String register(@Valid UserRegisterVo vos, BindingResult result,
                           RedirectAttributes attributes) {
        //如果有格式校验错误回到注册页面
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            attributes.addFlashAttribute("errors",errors);

            //效验出错回到注册页面
            return "redirect:http://auth.mall.com/reg.html";
        }

        // 远程注册
        //1、效验验证码  暂不使用验证码
        R r = memberFeignService.register(vos);
        if (r.getCode() == 0){
            // 成功
            return "redirect:http://auth.mall.com/login.html";
        } else {
            // 错误
            Map<String, String> errors = new HashMap<>();
            errors.put("msg", r.getData("msg",new TypeReference<String>(){}));
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.mall.com/reg.html";
        }
    }

    @GetMapping(value = "/login.html")
    public String loginPage(HttpSession session) {

        //从session先取出来用户的信息，判断用户是否已经登录过了
        Object attribute = session.getAttribute(LOGIN_USER);
        //如果用户没登录那就跳转到登录页面
        if (attribute == null) {
            return "login";
        } else {
            return "redirect:http://mall.com";
        }

    }

    @PostMapping(value = "/login")
    public String login(UserLoginVo vo, RedirectAttributes attributes, HttpSession session) {
        //远程登录
        R login = memberFeignService.login(vo);

        if (login.getCode() == 0) {
            MemberResponseVo data = login.getData("data", new TypeReference<MemberResponseVo>() {});
            session.setAttribute(LOGIN_USER,data);
            return "redirect:http://mall.com";
        } else {
            Map<String,String> errors = new HashMap<>();
            errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
            attributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.mall.com/login.html";
        }
    }


}
